package david.java.practice.test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: ZhaoDawei
 * @Date: Create in 10:28 上午 2022/11/10
 */
public class LC_0001_twoNumSum {

    public static void main(String[] args) {

        int[] arr = new int[]{2, 7, 11, 15};
        LC_0001_twoNumSum c = new LC_0001_twoNumSum();
        int[] ints = c.twoSum(arr, 9);
        System.out.println(ints[0] + "  "+ints[1]);

    }

    public  int[] twoSum(int[] arr, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (map.containsKey(target-arr[i])) {
                return new int[]{map.get(target - arr[i]), i};
            } else {
                map.put(arr[i], i);
            }

        }
        return null;
    }
}
